In this recipe we discuss handling the various versions of JavaScript when writing new scripts.Discussion
There are now three versions of JavaScript available to browser users: JavaScript, JavaScript 1.1, and JavaScript 1.2. The 1.0 version was shipped with Netscape Navigator 2.0. The 1.1 version shipped with Netscape Navigator 3.0, and the 1.2 version with Netscape Communicator 4.0. Microsoft Internet Explorer most closely approximates the JavaScript 1.1 specification.This means you have to tackle some interesting issues when you sit down to write a script for broad distribution. Using a favorite JavaScript 1.2 function or property means that you'll lock out anyone who doesn't have Netscape Communicator (or the upcoming Internet Explorer 4.0). Most often it means you'll be writing to the original JavaScript spec, just to make sure that as many people as possible can use your script.
Still, there are some features in JavaScript 1.2 that you're just going to have to use. They're just too useful to ignore. Your mission, then, becomes one of how to keep browsers that can't handle the new code from throwing a shoe when they hit your page.
If you've gotten this far, you can't have escaped that the <SCRIPT> tag has a LANGUAGE attribute (not the same as the translation language of the browser, given by navigator.language). Ordinarily you set the language attribute to 'JavaScript', but you can also set it to 'JavaScript1.1' or 'JavaScript1.2'. If you set the language attribute to one of these two values, you can keep browsers from trying to execute they shouldn't. The following table tells whether the given browser will attempt to execute scripts with the given language attributes:
JavaScript JavaScript 1.1 JavaScript 1.2 Navigator 2.0 Yes No No Navigator 3.0 Yes Yes No Communicator 4.0 Yes Yes Yes Internet Explorer 3.0 Yes Yes No Internet Explorer 4.0 Yes Yes Yes? Armed with this knowledge, you can build a base page which aims any given browser to an appropriately-versioned script:
<HTML> <BODY> <SCRIPT LANGUAGE="JavaScript1.2">location.href="thispage_12.htm"</SCRIPT> <SCRIPT LANGUAGE="JavaScript1.1">location.href="thispage_11.htm"</SCRIPT> <SCRIPT LANGUAGE="JavaScript">location.href="thispage_10.htm"</SCRIPT> Apparently you don't have a JavaScript-capable browser! </BODY> </HTML>You do the language attributes in 'descending' order because a JavaScript 1.2-capable browser would obey a plain <SCRIPT LANGUAGE='JavaScript'> tag and launch to the page with no JavaScript 1.2 code in it!
Copyright ©2000 by Charles River Media, All Rights Reserved